﻿Public Class Util

    Public Shared Function RemoveNaoNumericos(ByVal Str As String) As String
        Return Regex.Replace(Str, "[^\d]", String.Empty)
    End Function

    Public Shared Function ValidaCPF(ByVal cpf As String) As Boolean
        Dim i, x, n1, n2 As Integer
        Dim dadosArray() As String = {"111.111.111-11", "222.222.222-22", "333.333.333-33", "444.444.444-44", "555.555.555-55", "666.666.666-66", "777.777.777-77", "888.888.888-88", "999.999.999-99"}
        cpf = cpf.Trim
        For i = 0 To dadosArray.Length - 1
            If cpf.Length <> 14 Or dadosArray(i).Equals(cpf) Then
                Return False
            End If
        Next
        cpf = cpf.Substring(0, 3) + cpf.Substring(4, 3) + cpf.Substring(8, 3) + cpf.Substring(12)
        For x = 0 To 1
            n1 = 0
            For i = 0 To 8 + x
                n1 = n1 + Val(cpf.Substring(i, 1)) * (10 + x - i)
            Next
            n2 = 11 - (n1 - (Int(n1 / 11) * 11))
            If n2 = 10 Or n2 = 11 Then n2 = 0
            If n2 <> Val(cpf.Substring(9 + x, 1)) Then
                Return False
            End If
        Next
        Return True
    End Function

End Class
